import { IPackNifi } from "src/inter";
import Nifi from "../../nifi";
import {Cell} from "pack-antv-x6"
import markup from "./markup";
import { DEFAULT_CELL_NAME,DEFAULT_NODE_PORTS } from "../metadata";
import { pickData } from "../../util"
import {t} from "../../i18n/index"

const KEYS: [key: string, sk?: any][] = [
    ['id'],
    ['label', 'component.name'],
    ["position.x", "position.x"],
    ["position.y", "position.y"],
    ['version', 'revision.version']
];

const name = DEFAULT_CELL_NAME.funnels.en;
const shape = `node-${name}`;
//
export default (nifi:Nifi):IPackNifi.ModuleFunnels => {
    //
    nifi.graph.registerNode(shape,markup)
    //
    const ids:string[] = [];
    const init = (tds:any[]) => {
        const res: any[] = [];
        for (const td of new Array().concat(tds)) {
            if (!td) continue;
            ids.push(td.id)
            nifi.cells.set(td)
            const md: any = { ...pickData(td, KEYS),status:"RUN", name, shape,ports: { items: DEFAULT_NODE_PORTS.items } };
            res.push(md)
        }
        return res;
    }

    const done = () => {
        // for(const id of ids){
        //     const se = nifi.graph.getNeighbors(nifi.graph.getCellById(id),{incoming:true})?.[0]
        //     if(se){
        //         hookCall(nifi.cells.get(se),id)
        //         nifi.cells.addHook(se.id,id,hookCall)
        //     }
        // }
    }

    const hookCall = (sd:any,id:string) => {
        // const se = nifi.graph.getCellById(sd.id)
        // console.log('....',id,se)
        // if(se){
        //     const status = se.prop('status')
        //     nifi.graph.getCellById(id)?.prop({status})
        //     nifi.cells.connections.updateStatus(id);
        // }
        
    }
    
    // 新增元素
    const add = async (d:any) => {
        const data = { component:{...d}, disconnectedNodeAcknowledged: false, revision: { clientId: nifi.flow.clientId, version: 0 } }
        //
        const td = await nifi.cells.addCell(name, data)
        const md = init(td)[0];
        if(md){
            nifi.graph?.addNode(md);
        }
    }
    // 更新元素
    const set = async (data:any) => {}
    //
    const del = async (data:any) => {}
    //
    const refresh = async (node:Cell) => {}
    //
    const update = (data:any) => {}
    //
    const edit = (node:Cell) => {
        nifi.drawer.close();
    }
    const view = (node:Cell) => {}
    //
    const contextmenu = (node:Cell,evt:MouseEvent) => {}

    return {name,init,done,add,set,del,refresh,update,edit,view,contextmenu}
}